package com.lzw.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lzw.pojo.Student;
import com.lzw.pojo.User;
import com.lzw.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.management.modelmbean.ModelMBeanOperationInfo;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/UserController")
public class UserController {
    //声明业务接口对象
    @Autowired//自动装配
    private UserService us;
    @PostMapping("/login")
    //登录功能
    public String login(String uname, String upwd, HttpSession session){
       //调用业务方法
      User user=us.login(uname,upwd);
      //判断登录是否成功
        if(user!=null){
            //把用户信息保存到session
            session.setAttribute("uname",uname);
            //把用户的头像保存到session作用域中:
            session.setAttribute("uimage",user.getUimage());
            //重定向服务器
       return "redirect:/StudentController/getAllStu/aa";
        }
        session.setAttribute("error","账号密码错误");
        return "redirect:/index.jsp";
    }
    @PostMapping("/zhuce")
    public String zhuce(User user,
                        MultipartFile myFile,HttpSession session,
                        Model model)throws IOException{
        //要上传的文件名、
        String fileName=myFile.getOriginalFilename();
        //获取当前服务器路径
        String path=session.getServletContext().getRealPath("/images/");
        //项目本地路径
        String path1="F:/AABigProject/Project/src/main/webapp/images/";
        File projectFile=new File(path1+fileName);
        //创建新的文件夹
        File pathFile=new File(path);
        //判断文件夹是否存在
        if(!pathFile.exists()){
            //不存在
            pathFile.mkdir();
        }
        //创建项目上传后的文件
        File file=new File(pathFile,fileName);
        myFile.transferTo(file);
        myFile.transferTo(projectFile);
        //把上传文件的名字保存到数据库中:
        user.setUimage(fileName);
        boolean bo=us.ZhuCe(user);
        if(bo){
            return "index";
        }
        model.addAttribute("error","注册失败");
        return "zhuce";
    }
    @PostMapping("/getAlluname")
    public String getAlluname(String bo,Model model){
        //调用业务
        List<User> users=us.selectAllUname();
        model.addAttribute("users",users);
        model.addAttribute("bo",bo);
        return "success";
    }
    //验证账号
    @PostMapping(value = "/getUserByName/{uname}",produces ="application/json;charset=utf-8")
    @ResponseBody
    public String getUserByName(@PathVariable String uname){
        //判断账号是否存在:
        User user = us.getUserByName(uname);
        if(user !=null){//账号存在:
            //把User转换为json字符串:
            String jsonstr =  JSONObject.toJSONString(user);
            return jsonstr;
        }
        return  "{\"aa\":\"false\"}";//账号不存在
}

    //重置密码的请求:
    @PostMapping("/updateUser")
    public String updateUser(User user,
                             @PathVariable MultipartFile myfile,
                             HttpSession session) throws IOException {
        //获取要上传的文件的文件名:
        String fileName = myfile.getOriginalFilename();
        //获取当前的服务器路径:
        String path = session.getServletContext().getRealPath("/images/");
        //项目本地路径:
        String path1="F:/AABigProject/Project/src/main/webapp/images/";
        //把服务器路径封装成一个文件夹:
        File pathFile = new File(path);
        //判断文件夹是否存在:
        if(!pathFile.exists()){//文件夹不存在
            //新建文件夹
            pathFile.mkdirs();
        }
        //创建项目上传后的文件:
        File file = new File(pathFile,fileName);
        File projectFile = new File(path1+fileName);
        if(!file.exists()){
            //把文件内容上传到file里面:
            myfile.transferTo(file);
        }
        if(!projectFile.exists()){
            myfile.transferTo(projectFile);
        }
        user.setUimage(fileName);
        //调用修改的业务:
        us.updateUser(user);
        //重定向会登陆页面:
        return "redirect:/index.jsp";
    }
    //退出功能
    @GetMapping("/out")
    public String out(HttpSession session){
        session.removeAttribute("uname");
        session.removeAttribute("uimage");
        return "redirect:/index.jsp";
    }
}

